/**
 * 
 */
package com.toncen.samepms.fisher.formatter;

import com.toncen.samepms.system.dao.CodeDao;
import com.toncen.samepms.system.domain.Code;
import com.toncen.samepms.system.domain.CodeItem;
import com.sky.common.http.HTTPUtils;
import com.sky.common.string.StringUtils;
import com.sky.fisher.formatter.AbstractFormatter;
import com.sky.fisher.model.CommonColumnModel;

/**
 * 描述：编码格式化器
 * 
 * @author tch
 * 
 */
public class CodeFormatter extends AbstractFormatter {
	/**
	 * 编码参数名称
	 */
	public static String PARAMETER_CODE = "code";

	/**
	 * 
	 */
	public CodeFormatter() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sky.fisher.formatter.AbstractFormatter#format(com.sky.fisher.model.CommonColumnModel,
	 *      java.lang.String)
	 */
	@Override
	public String format(CommonColumnModel column, String source) throws Exception {
		if (!StringUtils.isEmpty(source)) {
			CodeDao codeDao = new CodeDao();
			String code = column.getParameterMap().get(CodeFormatter.PARAMETER_CODE);
			code = HTTPUtils.searchScopeValue(column.getGrid().getRequest(), code);// 从http范围搜索一下
			Code codeBean = codeDao.getByCode(code);
			if (codeBean == null) {
				throw new Exception("编码【" + code + "】不存在，请与系统管理员联系！");
			}
			for (CodeItem codeItem : codeBean.getCodeItemList()) {
				if (codeItem.getValue().equals(source)) {
					String result = codeItem.getName();
					if (!StringUtils.isEmpty(codeItem.getExtend1())//
							&& StringUtils.isEmpty(column.getParameterMap().get("edittype"))//
							&& StringUtils.isEmpty(column.getParameterMap().get("editname"))) {//
						result = "<span style='color:" + codeItem.getExtend1() + "'>" + codeItem.getName() + "</span>";
					}
					return result;
				}
			}
		}
		return "";
	}

}
